<?php
	class PHPFoxEngine{

		var $title;
		var $keywords;
		var $description;
		var $jQuery;
		var $data;
		var $database;
		var $dbFields;
		var $includes;
		var $globalTexts;
		var $jsincludes;
		
		function PHPFoxEngine($title="PHPFoxEngine Website",$keywords=NULL,$description=NULL,$session=false,$jQuery=false){
			$this->jsincludes = array();
			$this->data = parse_ini_file("PHPFoxEngine.ini");
			if($this->allIncludes){$this->require_all($this->classesDir);}else{$this->loadIncludes();$this->require_base();}
			if($this->db_address != ""){
				$this->dbFields = json_decode(file_get_contents("databaseFields.json"),true);
				$this->database = new Bd($this->db_address,$this->db_login,$this->db_pass,$this->db_base);
			}
			if($session)session_start();
			$this->loadGlobalTexts();
			$this->title = $title;
			$this->keywords = $keywords;
			$this->description = $description;
			$this->jQuery = $jQuery;
			require_once('./'.$this->functionsDir.'/sugar.php');
		}
		
		function require_all($rep,$mode='php'){
			if($mode=='css' && isset($_GET['categorie']) && $_GET['categorie'] == 'admin') return '<link rel="stylesheet" type="text/css" href="./css/admin.css"/>';
			$require="";
			if(is_dir('./'.$rep.'/')){
				$directory = opendir('./'.$rep.'/') or die('PHPFoxEngine : ouverture du dossier '.$rep.' impossible pour inclusion totale.');
				while(false !== ($file = readdir($directory))) {
					if($file != '.' && $file != '..' && !is_dir('./'.$rep.'/'.$file)) {
						if($mode=="php") require_once('./'.$rep.'/'.$file);
						if($mode=="css" && $file != 'admin.css') $require.= '<link rel="stylesheet" type="text/css" href="./'.$rep.'/'.$file.'"/>';
						if($mode=="js") $require.= '<script type="text/javascript" src="./'.$rep.'/'.$file.'"></script>';
					}
				}
				closedir($directory);
			}
			return $require;
		}
		
		function require_base(){
			for($i=0;$i<count($this->includes);$i++) require_once('./'.$this->classesDir.'/'.$this->includes[$i]);
		}
		
		function require_javascript(){
			$require = "";
			if(is_dir('./'.$this->javascriptDir.'/')){
				$directory = opendir('./'.$this->javascriptDir.'/') or die('PHPFoxEngine : ouverture du dossier '.$this->javascriptDir.' impossible pour inclusion du javascript.');
				foreach($this->jsincludes as $name => $value){
					$require.= '<script type="text/javascript" src="./'.$this->javascriptDir.'/'.$name.'"></script>';
				}
				closedir($directory);
			}
			return $require;
		}
		
		function include_js($javascriptFile){
			if(substr($javascriptFile,strlen($javascriptFile) - 4,3) != ".js") $javascriptFile .= '.js';
			$this->jsincludes[$javascriptFile] = "";
		}
		
		function head(){
			$head = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
			<meta content="text/html; charset='.$this->charset.'" http-equiv="Content-Type"/>
			<title>'.$this->title.'</title>';
			if($this->jQuery){ //Active le JQuery (ou pas)
				$head .= '<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2/jquery.min.js"></script>';
			}
			
			//$head .= $this->require_all($this->javascriptDir,"js");
			$head .= $this->require_all($this->cssDir,"css");
			
			if($this->keywords != NULL && is_array($this->keywords)){ //Gestion des mots clefs
				$head .= '<meta http-equiv="Keywords" content=" ';
				foreach ($this->keywords as $key){
					$head .= $key.',';
				}
				$head = substr($head,0,strlen($head)-1);
				$head .= '"/>';
			}
			
			if($this->description != NULL){ //Gestion de la description
				$head .= '<meta http-equiv="Description" content="'.$this->description.'"/>';
			}
			$head .= '</head>';
			return $head;
		}
		
		function getText($identifier){
			return isset($this->globalTexts->text->$identifier->fr)? $this->globalTexts->text->$identifier->fr:"PHPFoxEngine : le texte '".$identifier."' est introuvable";
		}
		
		function checkAdmin(){
			//return isset($_SESSION['admin']) && $_SESSION['admin'] == 1;
			return true;
		}
		
		function checkDatabase($adress,$login,$pass,$base){
			$bd = new Bd($adress,$login,$pass,$base);
			return $bd->connect();
		}
		
		function saveConfig(){
			return file_put_contents("PHPFoxEngineConfig.json",json_encode($this->data)) > 0;
		}
		
		function saveIniConfig()
		{
			$fp = fopen("PHPFoxEngine.ini","w") or die("<b>Erreur - Un fichier de configuration n'a pas pu être crée !</b>");
			foreach($this->data as $key => $value)
			{
				fwrite($fp, $key." = ".$value."\n");
			}
			return(TRUE);
		}
		
		function loadIncludes(){
			$this->includes = json_decode(file_get_contents("includes.json"));
		}
		
		function saveIncludes(){
			return file_put_contents("includes.json",json_encode($this->includes)) > 0;
		}
		
		function loadGlobalTexts(){
			$this->globalTexts = json_decode(file_get_contents("globalTexts.json"));
		}
		
		function saveGlobalTexts(){
			return file_put_contents("globalTexts.json",json_encode($this->globalTexts)) > 0;
		}
		
		function checkGlobaltext($id){
			foreach($this->globalTexts->lang as $lang => $flag){
				if($this->globalTexts->text->$id->$lang == "") return false;
			}
			return true;
		}
		
		function loadDbFields(){
			$this->dbFields = json_decode(file_get_contents("databaseFields.json"));
		}
		
		function saveDbFields(){
			return file_put_contents("databaseFields.json",json_encode($this->dbFields)) > 0;
		}
		
		public function query($query){
			try{
				if($this->database == NULL) throw new Exception("PHPFoxEngine : Aucune base de données configurée");
				return $this->database->query($query);
			}
			catch (Exception $e)
			{
				echo "PHPFoxEngine Error : ".$e->getMessage();
			}
		}
		
		public function __get($nomAttribut)
		{
			try{
				if(isset($this->$nomAttribut)){
					return $this->$nomAttribut;
				}
				if (array_key_exists($nomAttribut, $this->data)) {
					return $this->data[$nomAttribut];
				}
				throw new Exception("PHPFoxEngine : '".$nomAttribut."' n'est pas un attribut.");
			}
			catch (Exception $e)
			{
				echo "PHPFoxEngine Error : ".$e->getMessage();
			}
		}
		
		public function __set($nomAttribut, $valeurAttribut)
		{
			try{
				if(isset($this->$nomAttribut)){
					$this->$nomAttribut = $valeurAttribut;
					return true;
				}
				if (array_key_exists($nomAttribut, $this->data)) {
					$this->data[$nomAttribut] = $valeurAttribut;
					return true;
				}
				throw new Exception("PHPFoxEngine : '".$nomAttribut."' n'est pas un attribut.");
			}
			catch (Exception $e)
			{
				echo "PHPFoxEngine Error : ".$e->getMessage();
			}
		}
	}
?>